﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany
{
	public partial class organisation_form : Form
	{
		private eCompany.eObj.organisation m_org = null;
		private eCompany.eObj.currency_list m_cl = null;

		public organisation_form(ref eCompany.eObj.organisation org, ref eCompany.eObj.currency_list cl)
		{
			InitializeComponent();
			m_org = org;
			m_cl = cl;
            this.Text = m_org.name;
            LoadImageList();
			refreshScreen();
			InitGraph();
		}

		public organisation_form()
		{
			InitializeComponent();
			refreshScreen();
		}

        private void LoadImageList()
        {
            foreach (eCompany.eObj.currency cur in m_cl.currencies)
            {
                currencyImageList.Images.Add(cur.short_name, Tools.GetCurrencyImage(cur.short_name));
            }
        }

		private void refreshScreen()
		{
			double tot_gld = 0;
			double tot_hs_gld = 0;

			companyList.BeginUpdate();
			companyList.Items.Clear();

			eCompany.eManager.eManager eM = new eCompany.eManager.eManager();
			lblOrgName.Text = m_org.name;

			ListViewGroup grp = companyList.Groups.Add("", m_org.name);
			foreach (eCompany.eObj.account acc in m_org.account_list)
			{
				ListViewItem item = new ListViewItem();

				item.Group = grp;
				item.Text = "Account " + acc.currency;
                item.ImageKey = acc.currency;
				double acc_value = eM.getAccountValue(acc);
				double acc_gld_value = 0;
				double old_acc_value = eM.getAccountValue(acc,DateTime.Today.AddDays(-1));
				double old_gld_value = 0;

				if (acc.currency != "GLD")
				{
					acc_gld_value = eM.currencyToGOLD(m_cl, acc.currency, acc_value);
					old_gld_value = eM.currencyToGOLD(m_cl, acc.currency, old_acc_value);
				}
				else
				{
					acc_gld_value = acc_value;
					old_gld_value = old_acc_value;
				}

				tot_gld += acc_gld_value;
				tot_hs_gld += acc_gld_value;

				item.SubItems.Add("");
				item.SubItems.Add(eCompany.eManager.eManager.formatDouble(acc_value)+acc.currency+
					" ("+eCompany.eManager.eManager.formatDouble(acc_value-old_acc_value)+")");
				item.SubItems.Add(eCompany.eManager.eManager.formatDouble(acc_gld_value) + "G"+
					" ("+eCompany.eManager.eManager.formatDouble(acc_gld_value-old_gld_value)+")");
				companyList.Items.Add(item);
			}

			foreach (eCompany.eObj.company c in m_org.company_list)
			{
				grp = companyList.Groups.Add(c.id, c.name);
				
				foreach (eCompany.eObj.account acc in c.account_list)
				{
					ListViewItem item = new ListViewItem();

					item.Group = grp;
					item.Text = "Account " + acc.currency;
                    item.ImageKey = acc.currency;
					double acc_value = eM.getAccountValue(acc);
					double old_acc_value = eM.getAccountValue(acc, DateTime.Today.AddDays(-1));
					double acc_gld_value = 0;
					double old_gld_value = 0;
					if (acc.currency != "GLD")
					{
						acc_gld_value = eM.currencyToGOLD(m_cl, acc.currency, acc_value);
						old_gld_value = eM.currencyToGOLD(m_cl, acc.currency, old_acc_value);
					}
					else
					{
						acc_gld_value = acc_value;
						old_gld_value = old_acc_value;
					}
					tot_gld += acc_gld_value;
					tot_hs_gld += acc_gld_value;

					if (acc.id == c.default_account)
						item.Font = new System.Drawing.Font(item.Font.FontFamily, item.Font.Size, FontStyle.Bold);

					item.SubItems.Add("");
					item.SubItems.Add(eCompany.eManager.eManager.formatDouble(acc_value) + acc.currency +
						" ("+eCompany.eManager.eManager.formatDouble(acc_value-old_acc_value)+")");
					item.SubItems.Add(eCompany.eManager.eManager.formatDouble(acc_gld_value) + "G"+
						" ("+eCompany.eManager.eManager.formatDouble(acc_gld_value-old_gld_value)+")");
					companyList.Items.Add(item);
				}
				ListViewItem stock_item = new ListViewItem();
				stock_item.Group = grp;
				stock_item.Text = "Stock";
				double stock_value = eM.getStockValue(c);
				string comp_cur = eM.getAccount(c, Guid.Parse(c.default_account)).currency;
				double stock_gld_value = eM.currencyToGOLD(m_cl, comp_cur , stock_value);
				tot_gld += stock_gld_value;
				stock_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(eM.getStockCount(c))+" items");
				stock_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(stock_value) + comp_cur);
				stock_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(stock_gld_value) + "G");
				companyList.Items.Add(stock_item);

				foreach (eCompany.eObj.material_stock ms in c.material_list)
				{
					double raw_count = eM.getMaterialCount(c, ms.material_type);
					double raw_value = eM.getMaterialValue(c, ms.material_type);
					double raw_gld_value = eM.currencyToGOLD(m_cl, eM.getAccount(c, Guid.Parse(c.default_account)).currency, raw_value);
					tot_gld += raw_gld_value;

					ListViewItem raw_item = new ListViewItem();
					raw_item.Group = grp;
					raw_item.Text = "Raw " + ms.material_type.ToString();
					raw_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(raw_count)+" items");
					raw_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(raw_value) + eM.getAccount(c, Guid.Parse(c.default_account)).currency);
					raw_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(raw_gld_value) + "G");
					companyList.Items.Add(raw_item);
				}                

				foreach (eCompany.eObj.shop s in c.shops)
				{
					double shop_value = eM.getShopValue(c, s.currency);
					double shop_gld_value = eM.currencyToGOLD(m_cl, s.currency, shop_value);
					double shop_count = eM.getShopStock(c, s.currency);
					tot_gld += shop_gld_value;
					ListViewItem shop_item = new ListViewItem();
					shop_item.Group = grp;
					shop_item.Text = "Shop " + s.currency;
                    shop_item.ImageKey = s.currency;
					shop_item.SubItems.Add(eManager.eManager.formatDouble(shop_count)+" items");
					shop_item.SubItems.Add(eManager.eManager.formatDouble(shop_value) + s.currency);
					shop_item.SubItems.Add(eManager.eManager.formatDouble(shop_gld_value) + "G");
					companyList.Items.Add(shop_item);
				}
			}

			grp = companyList.Groups.Add("TOTAL", "Total");
			ListViewItem tot_item = new ListViewItem();
			tot_item.Text = "Without Stock";
			tot_item.Group = grp;
			tot_item.SubItems.Add("");
			tot_item.SubItems.Add("");
			tot_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(tot_hs_gld) + "G");
			companyList.Items.Add(tot_item);
			
			tot_item = new ListViewItem(); 
			tot_item.Text = "Stock included";
			tot_item.Group = grp;
			tot_item.SubItems.Add("");
			tot_item.SubItems.Add("");
			tot_item.SubItems.Add(eCompany.eManager.eManager.formatDouble(tot_gld)+"G");
			companyList.Items.Add(tot_item);

			companyList.SetGroupState(ListViewGroupState.Collapsible);
			companyList.EndUpdate();
		}

		private void addNewCompanyToolStripMenuItem_Click(object sender, EventArgs e)
		{
			((main)ParentForm).AddNewCompany();
		}

		private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
		{
			refreshScreen();
			RefreshGraph();
		}

		private void InitGraph()
		{
			eCompany.eManager.eManager eM = new eManager.eManager();
			List<eCompany.eManager.ChartItem> lst = eM.getOrganisationChartValue(m_org, m_cl);

			System.Windows.Forms.DataVisualization.Charting.Series serie = organisationChart.Series[0];
			foreach (eCompany.eManager.ChartItem item in lst)
			{
				serie.Points.AddXY(item.date, item.volume);
			}
			
		}

		private void RefreshGraph()
		{
			organisationChart.Series[0].Points.Clear();
			InitGraph();
		}
	}
}
